<?php

require_once 'libraries/Google/Client.php';
require_once 'libraries/Google/Service/Content.php';

class MKT_Google_Api_Shopping {
    private static $_client_id     = "631122200630-uc2c8hf8n2thkdb6l4rm3advbi84cb00.apps.googleusercontent.com",
            $_client_secret = "vURIJz753D9ZnDz113vGuV4F",
            $_redirect_uri  = "http://test14.elinext.com/wp-admin?gapi=1",
            $_app_name      = "mkt",
            $_merchant_id   = 100828451;
    
    
    public static function init() {
        
        if (isset($_GET['gapi'])) {
            self::authenticate();
        }
        
        add_action('publish_product', array(__CLASS__, 'handle_save_product'));
        
    }
    
    public static function authenticate() {
        if (!session_id()) {
            session_start();
        }
        
        $client = new Google_Client();
        $client->setApplicationName(self::$_app_name);
        $client->setClientId(self::$_client_id);
        $client->setClientSecret(self::$_client_secret);
        $client->setRedirectUri(self::$_redirect_uri);
        $client->setScopes('https://www.googleapis.com/auth/content');
        
        $_SESSION['google_shopping_client'] = $client;
        
        if ($client->getAccessToken()) {
            return true;
        }
        
        if (isset($_SESSION['oauth_access_token'])) {
            $client->setAccessToken($_SESSION['oauth_access_token']);
        } elseif (isset($_GET['code'])) {
            $token = $client->authenticate($_GET['code']);
            $_SESSION['oauth_access_token'] = $token;
        } else {
            header('Location: ' . $client->createAuthUrl());
        }
    }
    
    public static function handle_save_product() {
        if (!session_id()) {
            session_start();
        }
        
        if (! isset($_SESSION['google_shopping_client']) || ! $_SESSION['google_shopping_client']) {
            return false;
        }
        
        $post = get_post();
        
        $product = new Google_Service_Content($_SESSION['google_shopping_client']);
        
        $product->setOfferId($post['ID']);
        $product->setTitle($post['post_title']);
        $product->setDescription($post['post_content']);
        $product->setLink($post['guid']);
        $product->setImageLink('http://test14.elinext.com/wp-content/uploads/2014/06/Tiger-tigers-5091123-1280-800-150x150.jpg');
        $product->setContentLanguage('en');
        $product->setTargetCountry('GB');
        $product->setChannel('online');
        $product->setAvailability('in stock');
        $product->setCondition('new');
        //$product->setGoogleProductCategory('Media > Books');
        $product->setGtin('9780007350896');

        $price = new Google_Service_Content_Price();
        $price->setValue('2.50');
        $price->setCurrency('GBP');

        $shipping_price = new Google_Service_Content_Price();
        $shipping_price->setValue('0.99');
        $shipping_price->setCurrency('GBP');

        $shipping = new Google_Service_Content_ProductShipping();
        $shipping->setPrice($shipping_price);
        $shipping->setCountry('GB');
        $shipping->setService('Standard shipping');

        $shipping_weight = new Google_Service_Content_ProductShippingWeight();
        $shipping_weight->setValue(200);
        $shipping_weight->setUnit('grams');

        $product->setPrice($price);
        $product->setShipping(array($shipping));
        $product->setShippingWeight($shipping_weight);

        $result = $service->products->insert(self::$_merchant_id, $product);
    }
    
}


MKT_Google_Api_Shopping::init();

